package com.example.teatea.controller;

import com.example.teatea.dao.*;
import com.example.teatea.project.Good;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class OrderController implements Initializable {
    public TitledPane tea;
    public TitledPane cafe;
    public TitledPane rice;
    public TitledPane other;
    public Button lookBt;
    public Button orderBt;
    public Button returnBt;
    public Button changeBt;
    public ListView<Good> teaView;
    public ListView<Good> cafeView;
    public ListView<Good> riceView;
    public ListView<Good> otherView;
    public static Good good;
    ObservableList<Good> teaList = FXCollections.observableArrayList();
    ObservableList<Good> cafeList = FXCollections.observableArrayList();
    ObservableList<Good> riceList = FXCollections.observableArrayList();
    ObservableList<Good> otherList = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //设置下拉框收起动画
        tea.setAnimated(true);
        cafe.setAnimated(true);
        rice.setAnimated(true);
        other.setAnimated(true);
        //默认收起展开
        tea.setExpanded(true);
        cafe.setExpanded(false);
        rice.setExpanded(false);
        other.setExpanded(false);
        //列表显示
        initTea();
        initCafe();
        initRice();
        initOther();
        //商品列
        SeatController.hashMap = new GetGoodInMenu().getGoodInMenu(SeatController.nowSeat);
    }

    //茶列表显示
    public void initTea(){
        List<Good> goods = new GetGood().getGood(1);
        teaView.getSelectionModel().select(0);
        for(Good g : goods){
            teaList.add(g);
        }
        teaView.setItems(teaList);
        //获取每个单元的值
        teaView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {

            }
        });
        //点击事件
        teaView.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开商品信息
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    good = teaView.getSelectionModel().getSelectedItem();
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/GoodMessage.fxml"));
                    Scene scene = null;
                    try {
                        scene = new Scene(fxmlLoader.load());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    //设置窗口标题
                    stage.setTitle("商品信息");
                    //设置窗体不关闭不能使用其它窗体
                    stage.initModality(Modality.APPLICATION_MODAL);
                    //设置窗口大小不能改变
                    stage.setResizable(false);
                    stage.show();
                }
            }
        });
        //设置为单元工厂
        teaView.setCellFactory(new Callback<ListView<Good>, ListCell<Good>>() {
            @Override
            public ListCell<Good> call(ListView<Good> goodListView) {
                return new ListCell<>(){
                    protected void updateItem(Good item,boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hBox = new HBox();
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("/com/example/teatea/fxml/GoodList.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            GoodListController goodListController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            goodListController.setName(item.getgName());
                            goodListController.setPrice(item.getgPrice());
                            File file = new File("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\food\\" + item.getgImage());
                            Image img=new Image(file.toURI().toString());//设置图片
                            goodListController.setImage(img);
                            this.setGraphic(hBox);
                        }else{
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    //咖啡列表显示
    public void initCafe(){
        List<Good> goods = new GetGood().getGood(2);
        cafeView.getSelectionModel().select(0);
        for(Good g : goods){
            cafeList.add(g);
        }
        cafeView.setItems(cafeList);
        //获取每个单元的值
        cafeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {

            }
        });
        //点击事件
        cafeView.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开商品信息
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    good = cafeView.getSelectionModel().getSelectedItem();
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/GoodMessage.fxml"));
                    Scene scene = null;
                    try {
                        scene = new Scene(fxmlLoader.load());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    //设置窗口标题
                    stage.setTitle("商品信息");
                    //设置窗体不关闭不能使用其它窗体
                    stage.initModality(Modality.APPLICATION_MODAL);
                    //设置窗口大小不能改变
                    stage.setResizable(false);
                    stage.show();
                }
            }
        });
        //设置为单元工厂
        cafeView.setCellFactory(new Callback<ListView<Good>, ListCell<Good>>() {
            @Override
            public ListCell<Good> call(ListView<Good> goodListView) {
                return new ListCell<>(){
                    protected void updateItem(Good item,boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hBox = new HBox();
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("/com/example/teatea/fxml/GoodList.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            GoodListController goodListController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            goodListController.setName(item.getgName());
                            goodListController.setPrice(item.getgPrice());
                            File file = new File("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\food\\" + item.getgImage());
                            Image img=new Image(file.toURI().toString());//设置图片
                            goodListController.setImage(img);
                            this.setGraphic(hBox);
                        }else{
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    //便餐列表显示
    public void initRice(){
        List<Good> goods = new GetGood().getGood(3);
        riceView.getSelectionModel().select(0);
        for(Good g : goods){
            riceList.add(g);
        }
        riceView.setItems(riceList);
        //获取每个单元的值
        riceView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {

            }
        });
        //点击事件
        riceView.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开商品信息
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    good = riceView.getSelectionModel().getSelectedItem();
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/GoodMessage.fxml"));
                    Scene scene = null;
                    try {
                        scene = new Scene(fxmlLoader.load());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    //设置窗口标题
                    stage.setTitle("商品信息");
                    //设置窗体不关闭不能使用其它窗体
                    stage.initModality(Modality.APPLICATION_MODAL);
                    //设置窗口大小不能改变
                    stage.setResizable(false);
                    stage.show();
                }
            }
        });
        //设置为单元工厂
        riceView.setCellFactory(new Callback<ListView<Good>, ListCell<Good>>() {
            @Override
            public ListCell<Good> call(ListView<Good> goodListView) {
                return new ListCell<>(){
                    protected void updateItem(Good item,boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hBox = new HBox();
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("/com/example/teatea/fxml/GoodList.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            GoodListController goodListController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            goodListController.setName(item.getgName());
                            goodListController.setPrice(item.getgPrice());
                            File file = new File("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\food\\" + item.getgImage());
                            Image img=new Image(file.toURI().toString());//设置图片
                            goodListController.setImage(img);
                            this.setGraphic(hBox);
                        }else{
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    //纸牌列表显示
    public void initOther(){
        List<Good> goods = new GetGood().getGood(4);
        otherView.getSelectionModel().select(0);
        for(Good g : goods){
            otherList.add(g);
        }
        otherView.setItems(otherList);
        //获取每个单元的值
        otherView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {

            }
        });
        //点击事件
        otherView.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开商品信息
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    good = otherView.getSelectionModel().getSelectedItem();
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/GoodMessage.fxml"));
                    Scene scene = null;
                    try {
                        scene = new Scene(fxmlLoader.load());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    //设置窗口标题
                    stage.setTitle("商品信息");
                    //设置窗体不关闭不能使用其它窗体
                    stage.initModality(Modality.APPLICATION_MODAL);
                    //设置窗口大小不能改变
                    stage.setResizable(false);
                    stage.show();
                }
            }
        });
        //设置为单元工厂
        otherView.setCellFactory(new Callback<ListView<Good>, ListCell<Good>>() {
            @Override
            public ListCell<Good> call(ListView<Good> goodListView) {
                return new ListCell<>(){
                    protected void updateItem(Good item,boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hBox = new HBox();
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("/com/example/teatea/fxml/GoodList.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            GoodListController goodListController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            goodListController.setName(item.getgName());
                            goodListController.setPrice(item.getgPrice());
                            File file = new File("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\food\\" + item.getgImage());
                            Image img=new Image(file.toURI().toString());//设置图片
                            goodListController.setImage(img);
                            this.setGraphic(hBox);
                        }else{
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    public void toLook(ActionEvent actionEvent) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/OrderList.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        Stage stage = new Stage();
        stage.setScene(scene);
        //设置窗口标题
        stage.setTitle("已点商品清单");
        //设置窗体不关闭不能使用其它窗体
        stage.initModality(Modality.APPLICATION_MODAL);
        //设置窗口大小不能改变
        stage.setResizable(false);
        stage.show();
    }

    public void toOrder(ActionEvent actionEvent) {
        new AddMenu().addMenu(SeatController.hashMap,LoginController.s,SeatController.nowSeat);
        for(Good g:SeatController.hashMap.keySet()){
            new ChangeGood().changeGood(g,new CheckNum().checkNum(g.getgName()) - SeatController.hashMap.get(g));
        }
        Stage stage = (Stage) orderBt.getScene().getWindow();
        stage.close();
    }

    public void toReturn(ActionEvent actionEvent) {
        Stage stage = (Stage) returnBt.getScene().getWindow();
        stage.close();
    }

    public void toChange(ActionEvent actionEvent) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/ChangeSeat.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        Stage stage = new Stage();
        stage.setScene(scene);
        //设置窗口标题
        stage.setTitle("转台");
        //设置窗体不关闭不能使用其它窗体
        stage.initModality(Modality.APPLICATION_MODAL);
        //设置窗口大小不能改变
        stage.setResizable(false);
        stage.show();
    }
}